package cn.iocoder.yudao.module.train.service.viplevel;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.train.controller.admin.viplevel.vo.*;
import cn.iocoder.yudao.module.train.dal.dataobject.viplevel.VipLevelDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.train.dal.mysql.viplevel.VipLevelMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.train.enums.ErrorCodeConstants.*;

/**
 * 会员等级设定 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class VipLevelServiceImpl implements VipLevelService {

    @Resource
    private VipLevelMapper vipLevelMapper;

    @Override
    public Integer createVipLevel(VipLevelSaveReqVO createReqVO) {
        // 插入
        VipLevelDO vipLevel = BeanUtils.toBean(createReqVO, VipLevelDO.class);
        vipLevelMapper.insert(vipLevel);

        // 返回
        return vipLevel.getId();
    }

    @Override
    public void updateVipLevel(VipLevelSaveReqVO updateReqVO) {
        // 校验存在
        validateVipLevelExists(updateReqVO.getId());
        // 更新
        VipLevelDO updateObj = BeanUtils.toBean(updateReqVO, VipLevelDO.class);
        vipLevelMapper.updateById(updateObj);
    }

    @Override
    public void deleteVipLevel(Integer id) {
        // 校验存在
        validateVipLevelExists(id);
        // 删除
        vipLevelMapper.deleteById(id);
    }

    @Override
        public void deleteVipLevelListByIds(List<Integer> ids) {
        // 删除
        vipLevelMapper.deleteByIds(ids);
        }


    private void validateVipLevelExists(Integer id) {
        if (vipLevelMapper.selectById(id) == null) {
            throw exception(VIP_LEVEL_NOT_EXISTS);
        }
    }

    @Override
    public VipLevelDO getVipLevel(Integer id) {
        return vipLevelMapper.selectById(id);
    }

    @Override
    public PageResult<VipLevelDO> getVipLevelPage(VipLevelPageReqVO pageReqVO) {
        return vipLevelMapper.selectPage(pageReqVO);
    }

    @Override
    public List<VipLevelDO> getAllVipLevel() {
        return vipLevelMapper.selectAll(new VipLevelPageReqVO());
    }

}
